<template>
  <div class="overview">
    <div v-if="false">{{testData}}</div>
    <div class="row one">
      <a class="col-xs-8 user-info no-padding" href="javascript:;" @click="toSelfDetail">
        <img id="userHead" class="user-icon" :src="userHead">
        <div id="nickname" class="nickname">{{nickname}}</div>
      </a>
      <div class="col-xs-4 text-center gross-rate no-padding">
        <div class="key">总收益率</div>
        <div id="totalRate" class="value" :class="totalRate< 0 ? 'negative': 'positive'">
          {{totalRate}}%
        </div>
      </div>
    </div>
    <div class="row two">
      <div class="col-xs-7 no-padding">
        <div class="trim-losses">
          <span class="key">累计盈亏</span>
          <span  class="value" :class="totalFloatProfit<0?'negative':'positive'">{{totalFloatProfit}}</span>
        </div>
        <div class="total-assets">
          <span class="key">模拟资产</span>
          <span id="fundBalance" class="value">{{fundBalance}}</span>
        </div>
      </div>
      <div class="col-xs-5 text-right no-padding" @click.stop.prevent="toMedalsWall">
        <medals-owned :userType="userType" :medalsProps="medalsOwned"></medals-owned>
      </div>
    </div>
    <div class="row three">
      <div class="btn btn-simulate-trade btn-block" @click="toTrade">我的模拟交易</div>
    </div>
  </div>
</template>

<script>
  import MedalsOwned from 'components/dynamic/MedalsOwned'
  import LocalDAO from 'common/localDAO'
  import RemoteDAO from 'common/remoteDAO'
  import Constant from 'common/constant'
  import CommonUtil from 'common/common.util'
  import _ from 'underscore'

  export default {
    data () {
      return {
        title: '牛人牛股',
        nickname: LocalDAO.getNickname(),
        userHead: LocalDAO.getUserHead(),
        totalRate: '--',
        totalFloatProfit: '--',
        fundBalance: '--',
        dayRate: '--',
        userType: Constant.user.SELF,
        medalsOwned: LocalDAO.getMedalsOwned()||{list: []},
        testData: 'testData',
        error: ''
      }
    },
    created: function () {
      this.fetchData()
    },
    components: {
      'medals-owned': MedalsOwned
    },
    methods: {
      fetchData () {
        let self = this;
        let userCode = LocalDAO.getUserCode()
        //查询资产
        RemoteDAO.callJCL11103(userCode, function (response) {
          if (Number(response.error) === 0) {
            let data = response.data.body.data;
            if (data.length > 0) {
              self.totalRate = CommonUtil.toFixed(data[0].total_rate, 2); // 总收益率
              self.fundBalance = CommonUtil.toFixed(data[0].fund_balance, 2); // 模拟资产
              self.enableBalance = CommonUtil.toFixed(data[0].enable_balance, 2); // 可用资产
              self.dayRate = CommonUtil.toFixed(data[0].day_rate, 2); // 日盈亏率
              self.totalFloatProfit = CommonUtil.toFixed(data[0].total_floatprofit, 2); // 累计盈亏
              if(self.totalRate>0){
                self.totalRate = "+"+self.totalRate
              }
              if(self.totalFloatProfit>0){
                self.totalFloatProfit = "+"+self.totalFloatProfit
              }
              LocalDAO.setTotalRate(self.totalRate);
              LocalDAO.setDayRate(self.dayRate);
              LocalDAO.setTotalFloatProfit(self.totalFloatProfit);
              LocalDAO.setFundBalance(self.fundBalance);
              LocalDAO.setEnableBalance(self.enableBalance);
            }
          } else {
            self.$store.dispatch('setTip',{text:response.data});
          }
        });

        //查询用户资料
        RemoteDAO.callUC12000104(userCode, function (response) {
          if (Number(response.error) === 0) {
            let data = response.data.body[0];
            self.nickname = data.NICKNAME;
            if (data.USER_HEAD != "") {
              self.userHead = data.USER_HEAD;
            } else {
              self.userHead = CommonUtil.getDefaultUserHead(data.USER_CODE);
            }
            LocalDAO.setUserHead(self.userHead);
            LocalDAO.setNickname(self.nickname);
            LocalDAO.setWalletTotal(CommonUtil.toFixed(data.WALLET_TOTAL, 0));
            LocalDAO.setGoodpointCount(data.GOODPOINT_COUNT);
            LocalDAO.setSubscribeUserCount(data.SUBSCRIBE_USER_COUNT);

            let list = []; //已获勋章列表
            try {
              let honorList = JSON.parse(data.HONOR_LIST);
              _.each(honorList, function (item, index) {
                let object = {};
                object.medalCode = item.HONOR_CODE;
                object.medalName = item.HONOR_NAME;
                object.iconURL = "";
                object.walletNum = "";
                list.push(object);
              });
            } catch (err) {
              self.$store.dispatch('setTip', {text: "解析已获取勋章列表返回值失败"});
            }

            //查询所有勋章,获取各勋章详细资料
            RemoteDAO.callUC12000042(userCode, "", function (response) {
              if (Number(response.error) === 0) {
                _.each(response.data.body, function (item, index) {
                  let object = {};
                  object.iconURL = item.ICON_URL;  //勋章图地址
                  object.honorName = item.HONOR_NAME; //勋章名称
                  object.honorCode = item.HONOR_CODE; //勋章代码
                  object.configType = item.CONFIG_TYPE; //勋章类型
                  object.walletNum = item.CONFIG_VALUE.WALLET_NUM; //奖励君元宝数
                  object.taskDesc = item.CONFIG_VALUE.TASK_DESC; //任务说明
                  // 为 medalsOwned 赋值勋章图片路径和获得勋章奖励君元宝数
                  _.each(list, function (item, index) {
                    if (item.medalCode == object.honorCode) {
                      item.iconURL = object.iconURL;
                      item.walletNum = object.walletNum;
                      return false
                    }
                  });
                });

                //最后得到字段完全的已获勋章列表
                self.medalsOwned.list = list;
                LocalDAO.setMedalsOwned(self.medalsOwned);
              } else {
                self.$store.dispatch('setTip', {text: response.data});
              }
            });
          } else {
            self.$store.dispatch('setTip',{text:response.data});
          }
        });
      },
      toMedalsWall () {
        this.$router.push({ path: '/medalswall', query: { userType:Constant.user.SELF}});
        var kv = {"usercode": LocalDAO.getUserCode(),"k2":""};
        TDAPP.onEvent("14003","",kv);
        TDAPP.send();
      },
      toSelfDetail(){
        this.$router.push({path: '/selfdetail'});
        var kv = {"usercode": LocalDAO.getUserCode(), "k2": ""};
        TDAPP.onEvent("16008", "", kv);
        TDAPP.send();
      },
      toTrade () {
        this.$router.push({path: '/stockmain'});
        var kv = {"usercode": LocalDAO.getUserCode(),"k2":""};
        TDAPP.onEvent("14004","",kv);
        TDAPP.send();
      }
    }
  }
</script>

<style scoped>

</style>
